//
//  NewTabBarController.m
//  INMOTION
//
//  Created by cjg on 2019/9/9.
//  Copyright © 2019 Inmotion. All rights reserved.
//

#import "NewTabBarController.h"
#import "UIImage+Color.h"
#import "KSLanguageManager.h"
#import "IMTConfig.h"
#import "IMTNavigationController.h"
#import "LXCarHomeViewController.h"
#import "LXMoreTableViewController.h"
#import "AppTypeTool.h"

@interface NewTabBarController ()

@end

@implementation NewTabBarController

- (id)init {
    self = [super init];
    if (self) {
        
        
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *appName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
 
        //IMTNewCarLocalizedString(@"Vehicle")
        LXCarHomeViewController *carvc = [[LXCarHomeViewController alloc] initWithNibName:@"LXCarHomeViewController" bundle:nil];
        carvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:appName image:[[UIImage imageNamed:@"tarbar_notselect"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] selectedImage:[[UIImage imageNamed:@"tarbar_select"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]];
        
        
        /// 修改选中图标的颜色
        UIImage *moreImage = [[UIImage imageNamed:@"tab_more"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        
        LXMoreTableViewController *morevc = [[LXMoreTableViewController alloc] initWithNibName:@"LXMoreTableViewController" bundle:nil];
        morevc.tabBarItem = [[UITabBarItem alloc] initWithTitle:IMTNewCarLocalizedString(@"activity_main_tab_more") image:[[UIImage imageNamed:@"tab_more_no"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] selectedImage:moreImage];
        morevc.tableView.tintColor = [AppTypeTool getThemeColor];
        self.tabBar.tintColor = [AppTypeTool getThemeColor];
        self.tabBar.backgroundColor = kIMTLightBackgroundColor;

        /// 导航控制器
        IMTNavigationController *carNav = [[IMTNavigationController alloc] initWithRootViewController:carvc];
        [self setNavigations:carNav];
        
        IMTNavigationController *moreNav = [[IMTNavigationController alloc] initWithRootViewController:morevc];
        [self setNavigations:moreNav];
        
        self.viewControllers = @[carNav,moreNav];
    }
    return self;
}

- (void)setNavigations:(IMTNavigationController *)navController {
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc] init];
        //添加背景色
        appperance.backgroundColor = kIMTDefaultOrangeColor;
        appperance.shadowImage = [[UIImage alloc]init];
        appperance.shadowColor = nil;
        [appperance setTitleTextAttributes:@{NSForegroundColorAttributeName:kIMTDefaultTitleColor,
                                             NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:17]}];
        navController.navigationBar.standardAppearance = appperance;
        navController.navigationBar.scrollEdgeAppearance = appperance;
        navController.navigationBar.compactAppearance = appperance;
        navController.navigationBar.compactScrollEdgeAppearance = appperance;
    } else {
        navController.navigationBar.barTintColor = kIMTDefaultOrangeColor;
        navController.navigationBar.shadowImage = [UIImage new];
        [navController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : kIMTDefaultTitleColor,
                                                                          NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:17]}];
    }
}

- (UIImage*)imageWithColor:(UIColor*)color size:(CGSize)size {
    
    if(!color || size.width<=0|| size.height<=0)
        return nil;
    CGRect rect = CGRectMake(0.0f,0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size,NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)setBadgeAtIndex:(NSInteger)index count:(NSUInteger)count hidden:(BOOL)hidden {
    UITabBarItem *item = self.tabBar.items[index];
    if (count == 0) {
        item.badgeValue = nil;
    }else {
        item.badgeValue = [NSString stringWithFormat:@"%lu",(unsigned long)count];
    }
}

- (void)setSelectedTabIndex:(NSInteger)index {
    self.selectedIndex = index;
}


@end
